package main

import (
	"fmt"
	"net"
)

func main() {
	fmt.Println("ready to start server......")
	listen, err := net.Listen("tcp", "localhost:8888")
	if err != nil {
		fmt.Println("error happen", err.Error())
		return	//退出程序
	}

	for true {
		conn, err := listen.Accept()
		if err != nil {
			fmt.Println("accept req error.", err.Error())
			continue
		}
		go process(conn)
	}

}

func process(conn net.Conn) {
	for true {
		buf := make([]byte, 512)
		len, err := conn.Read(buf)
		if err != nil {
			fmt.Println("Error reading", err.Error())
			return //终止程序
		}
		fmt.Printf("Received data: %v", string(buf[:len]))
	}
}
